package com.lijy.awt;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author Lijingyu on 2017/10/19.
 */
public class RadioCheckNew extends Applet {

    TextField t = new TextField(30);

    Checkbox[] cb = {
            new Checkbox("Check Box 1"),
            new Checkbox("Check Box 2"),
            new Checkbox("Check Box 3") };

    CheckboxGroup g = new CheckboxGroup();
    Checkbox
            cb4 = new Checkbox("four", g, false),
            cb5 = new Checkbox("five", g, true),
            cb6 = new Checkbox("six", g, false);

    public void init() {
        t.setEditable(false);
        add(t);
        ILCheck il = new ILCheck();
        for(int i = 0; i < cb.length; i++) {
            cb[i].addItemListener(il);
            add(cb[i]);
        }
        cb4.addItemListener(new IL4());
        add(cb4);
        cb5.addItemListener(new IL5());
        add(cb5);
        cb6.addItemListener(new IL6());
        add(cb6);

    }

    public static void main(String[] args) {
        RadioCheckNew applet = new RadioCheckNew();
        Frame aFrame = new Frame("RadioCheckNew");
        aFrame.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
        aFrame.add(applet, BorderLayout.CENTER);
        aFrame.setSize(300,200);
        applet.init();
        applet.start();
        aFrame.setVisible(true);
    }

    private class ILCheck implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            for (int i = 0; i < cb.length; i++) {
                if (e.getSource().equals(cb[i])) {
                    t.setText("Check box " + (i + 1));
                    return;
                }
            }
        }
    }

    private class IL4 implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            t.setText("Radio button four");
        }
    }

    private class IL5 implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            t.setText("Radio button five");
        }
    }

    private class IL6 implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            t.setText("Radio button six");
        }
    }
}
